<?php
// $Id: buddy_api.views.inc,v 1.9.4.4 2008/08/01 17:44:49 nodestroy Exp $

/**
 * @file
 * Implements views functionality for buddy_api
 */
 
/**
 * Implementation of hook_views_tables()
 */
function buddy_api_views_tables() {
  global $user;
  
  // join from NODE -> USERNODE
  $tables['baj_usernode'] = array(
    'name' => 'usernode',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'nid'
      ),
      'right' => array(
        'field' => 'nid'
      ),
      'type' => 'inner',
    ),
    
  );
  
  // join from USERNODE -> BUDDYLIST
  $tables['baj_buddylist'] = array(
    'name' => 'buddylist',
    'join' => array(
      'left' => array(
        'table' => 'baj_usernode',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
      'type' => 'inner'
    ),
  );
  
  // join from BUDDYLIST -> USERS
  $tables['baj_users'] = array(
    'name' => 'users',
    'join' => array(
      'left' => array(
        'table' => 'baj_buddylist',
        'field' => 'buddy'
      ),
      'right' => array(
        'field' => 'uid'
      ),
      'type' => 'inner'
    ),
    'fields' => array(
      'name' => array(
        'name' => t('Buddylist: Users buddies'),
        'help' => t('helptext'),
        'handler' => 'views_handler_field_username', 
        'uid' => 'uid',
        'addlfields' => array('uid'),
      ),
    ), 
  );
  
  // join from usernode to buddylist_pending_requests
  $tables['baj_buddylist_pending_requests1'] = array(
    'name' => 'buddylist_pending_requests',
    'join' => array(
      'left' => array(
        'table' => 'baj_usernode',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'requester_uid'
      ),
      'type' => 'inner'
    ),
  );
  
  // join from buddylist_pending_requests to users
  $tables['baj_users1'] = array(
    'name' => 'users',
    'join' => array(
      'left' => array(
        'table' => 'baj_buddylist_pending_requests1',
        'field' => 'requestee_uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
      'type' => 'inner'
    ),
    'fields' => array(
      'name' => array(
        'name' => t('Buddylist: Requestees'),
        'help' => t('helptext'),
        'handler' => 'views_handler_field_username',
        'uid' => 'uid',
        'addlfields' => array('uid'),
      ),
      'deactivate' => array(
        'name' => t('Buddylist: Cancel Link'),
        'handler' => array(
          'buddy_api_views_handler_cancel_link' => t('Cancel link')
        ),
        'notafield' => TRUE,
        'query_handler' => 'buddy_api_views_field_query_handler',
        'help' => t('Display a link to cancel the request.'),     
      ),
    ),  
  );
  
  // join from usernode to buddylist_pending_requests
  $tables['baj_buddylist_pending_requests2'] = array(
    'name' => 'buddylist_pending_requests',
    'join' => array(
      'left' => array(
        'table' => 'baj_usernode',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'requestee_uid'
      ),
      'type' => 'inner'
    ),
    'fields' => array(
      'message' => array(
        'name' => t('Buddylist: Message'),
        'help' => t('helptext'),
      ),
    ),
  );
  
  // join from buddylist_pending_requests to users
  $tables['baj_users2'] = array(
    'name' => 'users',
    'join' => array(
      'left' => array(
        'table' => 'baj_buddylist_pending_requests2',
        'field' => 'requester_uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
      'type' => 'inner'
    ),
    'fields' => array(
      'name' => array(
        'name' => t('Buddylist: Requesters'),
        'help' => t('helptext'),
        'handler' => 'views_handler_field_username',
        'uid' => 'uid',
        'addlfields' => array('uid'),
      ),
      'accept' => array(
        'name' => t('Buddylist: Accept Link'),
        'handler' => array(
          'buddy_api_views_handler_accept_link' => t('Accept link')
        ),
        'notafield' => TRUE,
        'query_handler' => 'buddy_api_views_field_query_handler_ad',
        'help' => t('Display a link to accept the request.'),     
      ),
      'deny' => array(
        'name' => t('Buddylist: Deny Link'),
        'handler' => array(
          'buddy_api_views_handler_deny_link' => t('Deny link')
        ),
        'notafield' => TRUE,
        'query_handler' => 'buddy_api_views_field_query_handler_ad',
        'help' => t('Display a link to deny the request.'),     
      ),
    ),  
  );
   
  /*
   * joins for argument handling code
   */
   // join from node -> buddylist
  $tables['baj_node_buddylist'] = array(
    'name' => 'buddylist',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'buddy'
      ),
    ),
  );
  
   // join from buddylist -> users
   $tables['baj_node_buddylist_users'] = array(
    'name' => 'users',
    'join' => array(
      'left' => array(
        'table' => 'baj_node_buddylist',
        'field' => 'uid'
      ),
      'right' => array(
        'field' => 'uid'
      ),
    ),
    'fields' => array(
      'buddy_actionlink' => array(
        'name' => t('Buddylist: Actionlink'),
        'handler' => array(
          'buddy_api_views_handler_action_link' => t('Action link')
        ),
        'notafield' => TRUE,
        'help' => t('Display a action link to add/delete buddy.'),
      ),
    ),
    
    'filters' => array(
      'uid' => array(
        'name' => t('Buddylist: Author Is A Buddy Of Username'),
        'operator' => 'views_handler_operator_or',
        'list' => 'views_handler_filter_username',
        'value-type' => 'array',
        'help' => t('This allows you to filter by buddies of a particular user.'),
      ),
      'currentid' => array(
	    'field' => 'uid',
        'name' => t('Buddylist: Author Is A Buddy Of Current User'),
        'operator' => 'views_handler_operator_eqneq',
        'list' => 'views_handler_filter_usercurrent',
        'help' => t('This allows you to filter by buddies of the current user.'),
        'cacheable' => 'no',
      ),
    ), 
  );
  
  return $tables;
}

/**
 * query handler for cancel link
 */
function buddy_api_views_field_query_handler($fielddata, $fieldinfo, &$query) {
  $query->ensure_table('baj_buddylist_pending_requests1');
  $query->add_field('requestee_uid', 'baj_buddylist_pending_requests1');
  $query->add_field('requester_uid', 'baj_buddylist_pending_requests1');
}

/**
 * query handler for deny and accept link
 */
function buddy_api_views_field_query_handler_ad($fielddata, $fieldinfo, &$query) {
  $query->ensure_table('baj_buddylist_pending_requests2');
  $query->add_field('requestee_uid', 'baj_buddylist_pending_requests2');
  $query->add_field('requester_uid', 'baj_buddylist_pending_requests2');
}

/**
 * display a link to cancel the (own) request
 */
function buddy_api_views_handler_cancel_link($fieldinfo, $fielddata, $value, $data) {
  $link_text = $fielddata['options'] ? $fielddata['options'] : t('Cancel Request');
  return l($link_text, "buddylist/" . $data->requester_uid . "/buddies/request/cancel/" . $data->requestee_uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}

/**
 * display a link to accept the request
 */
function buddy_api_views_handler_accept_link($fieldinfo, $fielddata, $value, $data) {
  $link_text = $fielddata['options'] ? $fielddata['options'] : t('Accept Request');
  return l($link_text, "buddylist/" . $data->requestee_uid . "/buddies/requested/accept/" . $data->requester_uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}

/**
 * display a link to deny the request
 */
function buddy_api_views_handler_deny_link($fieldinfo, $fielddata, $value, $data) {
  $link_text = $fielddata['options'] ? $fielddata['options'] : t('Deny Request');
  return l($link_text, "buddylist/" . $data->requestee_uid . "/buddies/requested/deny/" . $data->requester_uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
}

/**
 * display a link to remove user from buddylist
 */
function buddy_api_views_handler_action_link($fieldinfo, $fielddata, $value, $data) {
  global $user;
  $viewed = user_load(array('uid' => $data->usernode_users_uid));
  
  $actions = buddylist_ui_get_buddy_actions($user, $viewed);
  $entry = each($actions);
  $firstentry = $entry['value'];
  
  return $firstentry;
}

/**
 * Implementation of hook_views_arguments
 */
function buddy_api_views_arguments() {  
    $arguments = array(
      'buddylist2_buddies' => array(
        'name' => t('Buddylist2: Nodes author is buddy of UID'),
        'handler' => 'buddy_api_views_handler_arg_buddies',
        'help' => t('This will filter nodes of all users, which are buddies of the given uid.'),
      ),
    );
    return $arguments;
}

/**
 * Callback for buddylist_views_arguments
 */
function buddy_api_views_handler_arg_buddies($op, &$query, $argtype, $arg = '') {
  switch($op) {
    case 'summary':
      $table_data = _views_get_tables();
      $joininfo = $table_data['baj_node_buddylist']['join'];
      $joininfo['type'] = 'inner';
      $query->add_table("baj_node_buddylist", true, 1, $joininfo);
      $fieldinfo['field'] = "users.name";
      return $fieldinfo;
      break;
    case 'sort':
      $query->add_orderby('node', 'title', $argtype);
      break;
    case 'filter':
      $uid = intval($arg);
      $table_data = _views_get_tables();
      $joininfo = $table_data['baj_node_buddylist']['join'];
      $joininfo['type'] = 'inner';
      $joininfo['extra'] = array('uid' => $uid);    
      $query->add_table("baj_node_buddylist", true, 1, $joininfo);
      break;
    case 'link':
      $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
      return l($name, "$arg/" . intval($query->uid));
    case 'title':
      if (!$query) {
        return variable_get('anonymous', 'Anonymous');
      }
      $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
      return check_plain($user->name);
  }
}

/**
 * Implementation of hook_views_default_views()
 */
function buddy_api_views_default_views() {
  // Default View for buddylist
         $view = new stdClass();
  $view->name = 'buddy_api_buddylist';
  $view->description = t('list of buddies (shows buddies of logged user - except on a other users page - integrated in the userpage)');
  $view->access = array (
);
  $view->view_args_php = 'global $user;
// Make the first argument the uid of the current user if not already set
if(arg(0) == "user" && is_numeric(arg(1))){
  $args[0] = arg(1);
}else{
  $args[0] = $user->uid;
}
return $args;';
  $view->page = TRUE;
  $view->page_title = t('Buddylist');
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'table';
  $view->url = 'buddylisting';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->sort = array (
  );
  $view->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'baj_users',
      'field' => 'name',
      'label' => '',
      'handler' => 'buddy_api_views_handler_field_username_link',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'usernode',
),
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(baj_users, node);

  
  // Default View for received requests (incoming requests)
     $view1 = new stdClass();
  $view1->name = 'buddy_api_received_requests';
  $view1->description = t('list of received requests (integrated in the buddylist page)');
  $view1->access = array (
);
  $view1->view_args_php = 'global $user;
// Make the first argument the uid of the current user if not already set
if (!$args[0]) {
  $args[0] = $user->uid;
}
return $args;';
  $view1->page = TRUE;
  $view1->page_title = t('Received requests');
  $view1->page_header = '';
  $view1->page_header_format = '1';
  $view1->page_footer = '';
  $view1->page_footer_format = '1';
  $view1->page_empty = '';
  $view1->page_empty_format = '1';
  $view1->page_type = 'table';
  $view1->url = '';
  $view1->use_pager = TRUE;
  $view1->nodes_per_page = '25';
  $view1->sort = array (
  );
  $view1->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view1->field = array (
    array (
      'tablename' => 'baj_users2',
      'field' => 'name',
      'label' => '',
      'handler' => 'buddy_api_views_handler_field_username_link',
    ),
    array (
      'tablename' => 'baj_buddylist_pending_requests2',
      'field' => 'message',
      'label' => '',
    ),
    array (
      'tablename' => 'baj_users2',
      'field' => 'accept',
      'label' => '',
      'handler' => 'buddy_api_views_handler_accept_link',
    ),
    array (
      'tablename' => 'baj_users2',
      'field' => 'deny',
      'label' => '',
      'handler' => 'buddy_api_views_handler_deny_link',
    ),
  );
  $view1->filter = array (
  );
  $view1->exposed_filter = array (
  );
  $view1->requires = array(baj_users2, baj_buddylist_pending_requests2);
  
  // Default View for sent requests (outgoing requests)
      $view2 = new stdClass();
  $view2->name = 'buddy_api_sent_requests';
  $view2->description = t('list of sent requests (integrated in the buddylist page)');
  $view2->access = array (
);
  $view2->view_args_php = 'global $user;
// Make the first argument the uid of the current user if not already set
if (!$args[0]) {
  $args[0] = $user->uid;
}
return $args;';
  $view2->page = TRUE;
  $view2->page_title = t('Sent Requests');
  $view2->page_header = '';
  $view2->page_header_format = '1';
  $view2->page_footer = '';
  $view2->page_footer_format = '1';
  $view2->page_empty = '';
  $view2->page_empty_format = '1';
  $view2->page_type = 'table';
  $view2->url = '';
  $view2->use_pager = TRUE;
  $view2->nodes_per_page = '25';
  $view2->sort = array (
  );
  $view2->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view2->field = array (
    array (
      'tablename' => 'baj_users1',
      'field' => 'name',
      'label' => '',
      'handler' => 'buddy_api_views_handler_field_username_link',
    ),
    array (
      'tablename' => 'baj_users1',
      'field' => 'deactivate',
      'label' => '',
      'handler' => 'buddy_api_views_handler_cancel_link',
    ),
  );
  $view2->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'usernode',
),
    ),
  );
  $view2->exposed_filter = array (
  );
  $view2->requires = array(baj_users1, node);

// Default View for buddylist (shows always the buddies of the logged user)
   $view3 = new stdClass();
  $view3->name = 'buddy_api_my_buddylist';
  $view3->description = t('list of my buddies (for block use - shows everytime the buddies of the logged user)');
  $view3->access = array (
);
  $view3->view_args_php = '';
  $view3->page = FALSE;
  $view3->page_title = '';
  $view3->page_header = '';
  $view3->page_header_format = '1';
  $view3->page_footer = '';
  $view3->page_footer_format = '1';
  $view3->page_empty = '';
  $view3->page_empty_format = '1';
  $view3->page_type = 'table';
  $view3->url = '';
  $view3->use_pager = TRUE;
  $view3->nodes_per_page = '10';
  $view3->block = TRUE;
  $view3->block_title = t('My Buddylist');
  $view3->block_header = '';
  $view3->block_header_format = '1';
  $view3->block_footer = '';
  $view3->block_footer_format = '1';
  $view3->block_empty = '';
  $view3->block_empty_format = '1';
  $view3->block_type = 'table';
  $view3->nodes_per_block = '10';
  $view3->block_more = TRUE;
  $view3->block_use_page_header = FALSE;
  $view3->block_use_page_footer = FALSE;
  $view3->block_use_page_empty = FALSE;
  $view3->sort = array (
  );
  $view3->argument = array (
  );
  $view3->field = array (
    array (
      'tablename' => 'users',
      'field' => 'name',
      'label' => '',
    ),
  );
  $view3->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'usernode',
),
    ),
    array (
      'tablename' => 'baj_node_buddylist_users',
      'field' => 'currentid',
      'operator' => '=',
      'options' => '',
      'value' => '***CURRENT_USER***',
    ),
  );
  $view3->exposed_filter = array (
  );
  $view3->requires = array(users, node, baj_node_buddylist_users);
 
// view for my buddies content
    $view4 = new stdClass();
  $view4->name = 'buddy_api_buddies_content';
  $view4->description = t('lists content which is created by a users buddy');
  $view4->access = array (
);
  $view4->view_args_php = '';
  $view4->page = FALSE;
  $view4->page_title = '';
  $view4->page_header = '';
  $view4->page_header_format = '1';
  $view4->page_footer = '';
  $view4->page_footer_format = '1';
  $view4->page_empty = '';
  $view4->page_empty_format = '1';
  $view4->page_type = 'table';
  $view4->url = '';
  $view4->use_pager = TRUE;
  $view4->nodes_per_page = '10';
  $view4->block = TRUE;
  $view4->block_title = t('My Buddies Content');
  $view4->block_header = '';
  $view4->block_header_format = '1';
  $view4->block_footer = '';
  $view4->block_footer_format = '1';
  $view4->block_empty = '';
  $view4->block_empty_format = '1';
  $view4->block_type = 'table';
  $view4->nodes_per_block = '10';
  $view4->block_more = FALSE;
  $view4->block_use_page_header = FALSE;
  $view4->block_use_page_footer = FALSE;
  $view4->block_use_page_empty = FALSE;
  $view4->sort = array (
  );
  $view4->argument = array (
  );
  $view4->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => '',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
    array (
      'tablename' => 'users',
      'field' => 'name',
      'label' => '',
    ),
  );
  $view4->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'page',
  1 => 'story',
),
    ),
    array (
      'tablename' => 'baj_node_buddylist_users',
      'field' => 'currentid',
      'operator' => '=',
      'options' => '',
      'value' => '***CURRENT_USER***',
    ),
  );
  $view4->exposed_filter = array (
  );
  $view4->requires = array(node, users, baj_node_buddylist_users);
  
  $views[$view->name] = $view;
  $views[$view1->name] = $view1;
  $views[$view2->name] = $view2;
  $views[$view3->name] = $view3;
  $views[$view4->name] = $view4;
  return $views;
}
